﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Crift
{
    
    public static class Conversions
    {
        public static string TRUE_BIT = "1";
        public static string StringToBits(string seed)
        {

            string key = string.Empty;
            for (int i = 0; i < seed.Length; i++)
            {
                byte s = Convert.ToByte(seed[i]);
                key += (Convert.ToString(s, 2));
            }
            return key;
        }

        public static bool BitTobool(char bit)
        {
            if (bit.Equals(TRUE_BIT)) return true; else return false;
        }

        public static byte[] BitArrayToByteArray(BitArray bitar)
        {

            byte[] temp = new byte[bitar.Length / 8];
            int bitscount = 7;
            int bytecount = 0;
            Byte tempbyte = 0;
            for (int i = 0; i < bitar.Length; i++)
            {
                if (bitar[i]) tempbyte += (byte)(Math.Pow(2, (7 - i % 8)));
                if (i == bitscount)
                {
                    temp[bytecount] = tempbyte;
                    bytecount++;
                    tempbyte = 0;
                    bitscount += 8;
                }
            }
            return temp;
        }
        public static BitArray ByteArrayToBitArray(byte[] x)
        {
            return new BitArray(x);
            
        }
    }
}
